<template>
  <div>
    <el-dialog title="监听器参数" v-model="dialogVisible" width="700px" :close-on-click-modal="false"
      :close-on-press-escape="false" @closed="$emit('close', formData.paramList)">
      <el-button type="primary" size="small" @click="addRow">新增</el-button>
      <el-form ref="form" :model="formData" :rules="rules">
        <el-table ref="tab" :data="formData.paramList">
          <el-table-column label="类型" width="180">
            <template #default="scope">
              <el-form-item :prop="'paramList.' + scope.$index + '.type'" :rules="rules.type">
                <el-select v-model="scope.row.type" placeholder="类型">
                  <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="名称" width="180">
            <template #default="scope">
              <el-form-item :prop="'paramList.' + scope.$index + '.name'" :rules="rules.name">
                <el-input v-model="scope.row.name" placeholder="名称" />
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="值" width="180">
            <template #default="scope">
              <el-form-item :prop="'paramList.' + scope.$index + '.value'" :rules="rules.value">
                <el-input v-model="scope.row.value" placeholder="值" />
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button @click="delRow(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="closeDialog"> 确 定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogVisible: true,
      formData: {
        paramList: this.value
      },
      types: [
        { label: '字符串', value: 'stringValue' },
        { label: '表达式', value: 'expression' }
      ],
      rules: {
        type: [
          { required: true, message: '请选择', trigger: ['blur', 'change'] },
        ],
        value: [
          { required: true, message: '请输入', trigger: ['blur', 'change'] },
        ],
        name: [
          { required: true, message: '请输入', trigger: ['blur', 'change'] },
        ],
      },
    }
  },
  computed: {
  },
  methods: {
    closeDialog() {
      this.$refs.form.validate().then(() => {
        this.dialogVisible = false
      }).catch(e => console.error(e))
    },
    addRow() {
      this.formData.paramList.push({})
    },
    delRow(idx) {
      this.formData.paramList.splice(idx, 1)
    }
  }
}
</script>

<style>
.flow-containers .el-badge__content.is-fixed {
  top: 18px;
}
</style>
